package com.controller;

import com.bean.*;
import com.service.BookingService;
import com.service.CdyOrderService;
import com.service.CommodityService;
import com.service.MovieService;
import com.util.PageAssistant;
import com.vo.FilmsVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpSession;
import java.util.Calendar;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("order")
public class OrderController {
    @Autowired
    private BookingService bookingService;
    @Autowired
    private CdyOrderService cdyOrderService;
    @Autowired
    private MovieService movieService;
    @Autowired
    private CommodityService commodityService;

    @RequestMapping("query")
    public String queryOrder(HttpSession session, Model model, Booking booking,PageAssistant<Booking> paBook,Movie movieQuery){
        System.out.println(">>>>>>>>> orderMovie query ");
        System.out.println("-------------------");
        Map<String,Object> map = new HashMap<>();

        map.put("start",paBook.getStart());
        map.put("size",paBook.getSize());

        paBook.setCount(bookingService.count(map));
        paBook.setItems(bookingService.query(map));

        if (movieQuery.getState() == null){
            movieQuery.setId(null);
            movieQuery.setState(1);
        }
        List<Movie> movies = movieService.find(movieQuery);
        System.out.println(movieQuery);

        model.addAttribute("paBook",paBook);
        model.addAttribute("movies",movies);
        System.out.println(paBook.getItems());

        return "page/order/bookOrderList";
    }
    @RequestMapping("queryCdy")
    public String queryCdy(HttpSession session, Model model, CdyOrder cdyOrder, PageAssistant<CdyOrder> paCdy, Commodity commodity){
        System.out.println(">>>>>>>>> cdy query ");
        System.out.println(commodity);
        System.out.println("-------------------");
        Map<String,Object> map = new HashMap<>();

        map.put("start",paCdy.getStart());
        map.put("size",paCdy.getSize());

        paCdy.setCount(cdyOrderService.count(map));
        paCdy.setItems(cdyOrderService.query(map));

        if (commodity.getState() == null){
            commodity.setState(0);
            commodity.setId(null);
        }
        List<Commodity> commodityList = commodityService.find(commodity);
        System.out.println(commodityList);

        model.addAttribute("paCdy",paCdy);
        model.addAttribute("commodityList",commodityList);
        System.out.println(paCdy.getItems());
        System.out.println(paCdy.getCurr());

        return "page/order/cdyOrderList";
    }

    @RequestMapping("queryMovieT")
    public String queryMovieT(HttpSession session, Model model, Booking booking,Movie movie){
        System.out.println(">>>>>>>>> MovieT query ");
        System.out.println(movie);
        System.out.println("-------------------");
        Map<String,Object> map = new HashMap<>();

        map.put("movieName",movie.getId());
        System.out.println(map);

        int count = bookingService.count(map);

        List<Movie> countMovie = movieService.find(movie);
        model.addAttribute("count",count);
        model.addAttribute("countMovie",countMovie.get(0));

        return "page/order/bookOrderCount";
    }

    @RequestMapping("queryCdyT")
    public String queryCdyT(HttpSession session, Model model, CdyOrder cdyOrder , Commodity commodity){
        System.out.println(">>>>>>>>> CdyT query ");
        System.out.println(cdyOrder);
        System.out.println(commodity);
        System.out.println("-------------------");
        Map<String,Object> map = new HashMap<>();

        map.put("cdyName",commodity.getId());
        System.out.println(map);

        List<Commodity> commodities = commodityService.find(commodity);
        //选择商品的集合
        List<CdyOrder> cdyOrders = cdyOrderService.query(map);
        int total = 0;

        for (int i = 0; i <cdyOrders.size() ; i++) {
            total += cdyOrders.get(i).getNumber();
        }
        System.out.println(total);

        int count = cdyOrderService.count(map);

        model.addAttribute("total",total);
        model.addAttribute("commoditie",commodities.get(0));

        return "page/order/commodityOrderCount";
    }
    @RequestMapping("day")
    public String day(HttpSession session, Model model,Booking booking,Movie movie){
        System.out.println(">>>>>>>>> day ");
        System.out.println(booking);
        System.out.println(movie);
        System.out.println("-------------------");
        movie.setState(1);
        List<Movie> movies = movieService.find(movie);
        model.addAttribute("movies",movies);
        System.out.println(movies);
        return "page/ticketCount/day";
    }
    @RequestMapping("dayCount")
    public String dayCount(HttpSession session, Model model,Booking booking,Movie movie){
        System.out.println(">>>>>>>>> dayCount ");
        System.out.println(booking);
        System.out.println(movie);
        System.out.println("-------------------");

        Map<String,Object> map = new HashMap<>();
        map.put("movieName",movie.getId());
        //查询所选择电影的所有订票信息
        List<Booking> bookings = bookingService.query(map);
        System.out.println(bookings);
        int total = 0;
        int sumMoney = 0;
        //获取日历工具
        Calendar cal = Calendar.getInstance();
        //获取当前天数
        int day = cal.get(Calendar.DATE);
        for (int i = 0; i <bookings.size() ; i++) {
            //判断是否为当天
            System.out.println("记录:"+bookings.get(i).getTime().getDate());
            System.out.println("当天:"+day);
            if (bookings.get(i).getTime().getDate() == day){
                total += 1;
                sumMoney += bookings.get(i).getPrice();
            }
        }
        System.out.println(total);
        System.out.println(sumMoney);
        model.addAttribute("total",total);
        model.addAttribute("sumMoney",sumMoney);
        model.addAttribute("bookings",bookings.get(0));
        return "page/ticketCount/dayCount";
    }
    @RequestMapping("week")
    public String week(HttpSession session, Model model,Booking booking,Movie movie){
        System.out.println(">>>>>>>>> week ");
        System.out.println(booking);
        System.out.println(movie);
        System.out.println("-------------------");
        movie.setState(1);
        List<Movie> movies = movieService.find(movie);
        model.addAttribute("movies",movies);
        System.out.println(movies);
        return "page/ticketCount/week";
    }
    @RequestMapping("weekCount")
    public String weekCount(HttpSession session, Model model, Booking booking,Movie movie){
        System.out.println(">>>>>>>>> weekCount ");
        System.out.println(booking);
        System.out.println(movie);
        System.out.println("-------------------");

        Map<String,Object> map = new HashMap<>();
        map.put("movieName",movie.getId());
        //查询所选择电影的所有订票信息
        List<Booking> bookings = bookingService.query(map);
        int total = 0;
        int sumMoney = 0;
        //获取日历工具
        Calendar cal = Calendar.getInstance();
        //获取当前天数
        int day = cal.get(Calendar.WEEK_OF_MONTH);
        for (int i = 0; i <bookings.size() ; i++) {
            //判断是否为当天
            Calendar cal1 = Calendar.getInstance();
            //给新的Calender赋值
            cal1.setTime(bookings.get(i).getTime());
            System.out.println("记录:"+cal1.get(Calendar.WEEK_OF_MONTH));
            System.out.println("当天:"+day);
            if (cal1.get(Calendar.WEEK_OF_MONTH) == day){
                total += 1;
                sumMoney += bookings.get(i).getPrice();
            }
        }
        System.out.println(total);
        System.out.println(sumMoney);
        model.addAttribute("total",total);
        model.addAttribute("sumMoney",sumMoney);
        model.addAttribute("bookings",bookings.get(0));
        return "page/ticketCount/weekCount";
    }

    @RequestMapping("month")
    public String month(HttpSession session, Model model,Booking booking,Movie movie){
        System.out.println(">>>>>>>>> month ");
        System.out.println(booking);
        System.out.println(movie);
        System.out.println("-------------------");
        //若未选择查询
        movie.setState(1);
        System.out.println(movie+"<><><新加的属性");
        List<Movie> movies = movieService.find(movie);
        model.addAttribute("movies",movies);
        System.out.println(movies);
        return "page/ticketCount/month";
    }

    @RequestMapping("monthCount")
    public String monthCount(HttpSession session, Model model, Booking booking,Movie movie){
        System.out.println(">>>>>>>>> monthCount ");
        System.out.println(booking);
        System.out.println(movie);
        System.out.println("-------------------");

        Map<String,Object> map = new HashMap<>();
        map.put("movieName",movie.getId());
        //查询所选择电影的所有订票信息
        List<Booking> bookings = bookingService.query(map);
        System.out.println(bookings.size()+"><><><><><><><<><>票数");
        int total = 0;
        int sumMoney = 0;
        //获取日历工具
        Calendar cal = Calendar.getInstance();
        //获取当前天数
        int day = cal.get(Calendar.MONTH);
        for (int i = 0; i <bookings.size() ; i++) {
            //判断是否为当天
            System.out.println("记录:"+bookings.get(i).getTime().getMonth());
            System.out.println("当天:"+day);
            if (bookings.get(i).getTime().getMonth() == day){
                total += 1;
                sumMoney += bookings.get(i).getPrice();
            }
        }
        System.out.println(total);
        System.out.println(sumMoney);
        model.addAttribute("total",total);
        model.addAttribute("sumMoney",sumMoney);
        model.addAttribute("bookings",bookings.get(0));
        return "page/ticketCount/monthCount";
    }
}
